﻿using System;
using System.Web.Mvc;
using DotNetDream.Models;

namespace DotNetDream.Helper
{
    public static class DndHtmlHelper
    {
        /// <summary>
        /// Create a view account link
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="userGuid"></param>
        /// <returns></returns>
        public static MvcHtmlString ViewAccountLink(this HtmlHelper helper, Guid userGuid)
        {
            //Get the instance of specified user
            DotNetDreamEntities db = new DotNetDreamEntities();
            BlogUser user = db.BlogUsers.Find(userGuid);
            //Build the anchor tag
            var builder = new TagBuilder("a");
            builder.MergeAttribute("href", string.Format("/Account/ViewAccount/{0}", Operation.Encrypt(userGuid.ToString())), true);
            builder.InnerHtml = user.FirstName + " " + user.LastName;
            //Then return the anchor HTML
            return MvcHtmlString.Create(builder.ToString());
        }

        /// <summary>
        /// Create an image
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="imageUrl"></param>
        /// <param name="altText"></param>
        /// <returns></returns>
        public static MvcHtmlString Image(this HtmlHelper helper, string imageUrl, string altText)
        {
            var builder = new TagBuilder("img");
            builder.MergeAttribute("src", imageUrl);
            builder.MergeAttribute("alt", altText);
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
        }

        /// <summary>
        /// Create an image with predefined size
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="imageUrl"></param>
        /// <param name="altText"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static MvcHtmlString Image(this HtmlHelper helper, string imageUrl, string altText, int width, int height)
        {
            var builder = new TagBuilder("img");
            builder.MergeAttribute("src", imageUrl);
            builder.MergeAttribute("alt", altText);
            builder.MergeAttribute("width", width.ToString());
            builder.MergeAttribute("height", height.ToString());
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
        }
    }
}
